//
//  UtilityFunc.m
//  LMMBBT
//
//  Created by cruise on 15/6/24.
//  Copyright (c) 2015年 com.lmmb. All rights reserved.
//

#import "UtilityFunc.h"

@implementation UtilityFunc

+ (BOOL) stringEmpty:(NSString *)string {
	if (string == nil || string == NULL) {
		return YES;
	}
	if ([string isKindOfClass:[NSNull class]]) {
		return YES;
	}
	if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
		return YES;
	}
	return NO;
}

//判断字符串时候为空
+(BOOL) stringNotEmpty:(NSString *) str {
	return str && (str.length > 0);
}

//判断数组是否为空
+(BOOL) arrayNotEmpty:(NSArray *) arr {
	return arr && (arr.count > 0);
}

+(id) stringDataConverter:(NSString *) string {
	NSError *error = nil;
	
	NSData *data= [string dataUsingEncoding:NSUTF8StringEncoding];
	
	id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
	
	return jsonObject;
}

+(NSDictionary *) getWeek {
	NSMutableArray *newArray = [NSMutableArray new];
	
	NSDate *today = [NSDate date];
	NSCalendar *calendar = [NSCalendar currentCalendar];
	
	NSDateComponents *components = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSDayCalendarUnit fromDate:today];
	
	NSInteger day = [components day];
	NSInteger weekDay = [components weekday];
	
	NSInteger weekFirstDay = day-(weekDay-1);
	
	NSArray *weekArray = @[ @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六" ];
	
	for (int i = 0; i < 7; i++) {
		NSInteger someDay = weekFirstDay+i;
		
		NSDateComponents *firstDayComp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
		[firstDayComp setDay:someDay];
		NSDate *date = [calendar dateFromComponents:firstDayComp];
		
		NSDateComponents *newComp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSDayCalendarUnit fromDate:date];
		
		NSInteger currentDay = [newComp day];
		
		NSInteger currentMonth = [newComp month];
		
		NSString *dateString = [NSString stringWithFormat:@"%@\n%ld.%ld", weekArray[i], currentMonth, currentDay];
		
		[newArray addObject:dateString];
	}
	
	return @{@"week":newArray, @"weekDay": [NSString stringWithFormat:@"%ld", weekDay-1]};
}

+(NSArray *) getTimeWithData:(NSDictionary *) dict {
	NSMutableArray *newArray = [NSMutableArray new];
	NSDictionary *timeDict;
	for (int i = 8; i < 24; i++) {
		NSString *key = [NSString stringWithFormat:@"hour%d", i];
		NSString *timeStr = @"";
		NSString *selected = @"";
		
		selected = dict[key];
		
		if (![self stringNotEmpty:dict[key]]) {
			selected = @"0";
		}
		
		if (i<10) {
			timeStr = [NSString stringWithFormat:@"0%d : 00", i];
		} else {
			timeStr = [NSString stringWithFormat:@"%d : 00", i];
		}
		
		timeDict = @{@"time":timeStr, @"selected":selected};
		
		[newArray addObject:timeDict];
	}
	
	return newArray;
}

+(NSMutableArray *) enumByDictionary:(NSDictionary *)dictionary {
	NSMutableArray *tempArray = [NSMutableArray new];
	NSEnumerator *tempEnumerator = [dictionary objectEnumerator];
	NSObject *next;
	while (next = [tempEnumerator nextObject]) {
		[tempArray addObject:next];
	}
	return tempArray;
}

@end
